package com.brother.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.log4j.Logger;

import com.brother.database.DataBase;
import com.brother.geocode.GeoCoder;
import com.brother.ibatis.ClientFactory;
import com.brother.model.AlarmType;
import com.brother.model.ConnectionTip;
import com.brother.model.DataFlag;
import com.brother.model.LatLngAddress;
import com.brother.model.RealTimeData;
import com.brother.model.Report;
import com.brother.model.Signal;
import com.brother.model.Tracker;
import com.brother.realtime.cache.GeoDataOutput;
import com.brother.util.MalaysiaAddress;
import com.brother.util.NigeriaAddress;
import com.ibatis.common.util.PaginatedList;
import com.ibatis.sqlmap.client.SqlMapClient;

public class RealDataService {
private static Logger log = Logger.getLogger(RealDataService.class);
private ReportService reportSrv = new  ReportService();
public PaginatedList findData(String pageNo,int pageLimit,RealTimeData search){
	PaginatedList paginated = null;
	
	SqlMapClient map = ClientFactory.getSqlMapper();
	
	try {
		paginated = map.queryForPaginatedList("selectRealTimeDataByExample", search, pageLimit);
		paginated.gotoPage(Integer.parseInt(pageNo));
	} catch (SQLException e) {
		log.error("", e);
	}
	return paginated;
}	

public boolean deleteAlarming(int realtimedataid){
	SqlMapClient map = ClientFactory.getSqlMapper();
	
	try {
		 return map.update("deleteAlarming", realtimedataid) > 0;
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	return false;
}

public int findDataSize(int pageLimit,RealTimeData search){
	int size = 0;
	
	SqlMapClient map = ClientFactory.getSqlMapper();
	
	try {
		size = (Integer) map.queryForObject("selectCountRealTimeDataByExample", search, pageLimit);
	} catch (SQLException e) {
		log.error("", e);
	}
	return size;	
}

public void insertData(RealTimeData data){
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		Date td = data.getTraceDateTime();
		Date tommorrow = new Date();
		tommorrow.setDate(tommorrow.getDate() + 1);
		if(td.after(tommorrow)){
			//  超过当天时间的不记录数据库 
			log.info("data format error! trackerid="+data.getTrackerid());
			return;
		}
		LatLngAddress addr = GeoCoder.getAddress((data.getLatOrient().equals("S")? -data.getLat(): data.getLat())+"",
				(data.getLngOrient().equals("W")? -data.getLng():data.getLng())+"", data.getLanguage());
		if(addr != null){
			data.setAddress(addr.getAddress());
		}else{
	        int lngSign = data.getLngOrient().equals("W") ? -1 : 1;
	        int latSign = data.getLatOrient().equals("S") ? -1 : 1;
	        String address = null;
	        if(NigeriaAddress.getInstance().isIntesect(data.getLat()*latSign, data.getLng()*lngSign))
	        	address = NigeriaAddress.getInstance().getAddressByDB(data.getLat()*latSign, data.getLng()*lngSign);
	        else if(MalaysiaAddress.getInstance().isIntesect(data.getLat()*latSign, data.getLng()*lngSign)){
	        	address = MalaysiaAddress.getInstance().getAddressByDB(data.getLat()*latSign, data.getLng()*lngSign);
	        }
	        if(address != null){
	        	data.setAddress(address);
	        	GeoCoder.saveAddress((data.getLat()*latSign) + "", (data.getLng()*lngSign)+ "", address, "en", null,null);
	        }
		}
		
		map.insert("insertRealTimeData", data);
		this.insertReport(data);
		
		RealTimeData search = new RealTimeData();
		search.setTrackerid(data.getTrackerid());
		search.setOrderCondition(" order by traceTime desc ");
		search.setTopCondition(" limit 0,1 ");
		List<RealTimeData> rl = findData("0", 1, search);
		if(rl.size() != 0){
			data = rl.get(0);
			GeoDataOutput.getInstance().setRealtimeData(data.getTrackerid(), this.getDataString(data));
			if(!data.getState().equals("")){ // 有报警
				GeoDataOutput.getInstance().setAlarmData(data.getTrackerid(), this.getDataString(data));
			}
		}
		
	} catch (SQLException e) {
		log.error("", e);
	}finally{
		try {
			map.endTransaction();
		} catch (SQLException e) {
			log.error("", e);
		}
	}
}

/**首先是否已经符合条件时间间隔，插入一条报告信息
 * @param data
 */
private void insertReport(RealTimeData data) {
	//System.out.println("insert report data:" + data.getAddress());
	TrackerService tks = new TrackerService();
	Tracker tracker = tks.getTrackerByFieldName("trackerid", data.getTrackerid());
	
	SqlMapClient map = ClientFactory.getSqlMapper();
	Report report = new Report();
	report.setOrderCondition(" order by r.createTime desc");
	report.setTopCondition(" limit 0,1");
	report.setTrackerid(data.getTrackerid());
	try {
		
			report.setTrackerid(data.getTrackerid());
			report.setCreateTime(data.getTraceDateTime());
			report.setAddress(data.getAddress());
			report.setComment(data.getComment());
			report.setLat(data.getLat());
			report.setLng(data.getLng());
			if(data.getLatOrient().equals("S")){
				report.setSignedLat(-Math.abs(data.getLat()));
			}else{
				report.setSignedLat(data.getLat());
			}
			if(data.getLngOrient().equals("W")){
				report.setSignedLng(-Math.abs(data.getLng()));
			}else{
				report.setSignedLng(data.getLng());
			}
			report.setOrig_lat(data.getOrig_lat());
			report.setOrig_lng(data.getOrig_lng());
			report.setLatOrient(data.getLatOrient());
			report.setLngOrient(data.getLngOrient());
			report.setOrient(data.getOrient());
			report.setSignal(data.getSignal());
			report.setState(data.getState());
			report.setSpeed(data.getSpeed());
			report.setLanguage(data.getLanguage());
			report.setRealTimedataId(data.getRealTimedataId());
			
			report.setBattery(data.getBattery());
			report.setVoltage(data.getVoltage());
			report.setFuel(data.getFuel());
			report.setFlag(data.getFlag());
			report.setAd2(data.getAd2());
			report.setTemp(data.getTemp());
			
			if(report.getAddress() == null){
				//LatLngAddress addr = GeoCoder.getAddress(data.getLat()+"", data.getLng()+"", data.getLanguage());
				//System.out.println("set address in insertData insert report  858888888888888888888888"+addr);
				//report.setAddress(addr == null ? null: addr.getAddress());
			}
			//map.insert("insertReport",report);
			this.reportSrv.insertData(report, tracker);
	} catch (Exception e) {
		Logger.getLogger(this.getClass()).error("",e);
	}finally{
		try {
			map.endTransaction();
		} catch (SQLException e) {
			log.error("", e);
		}
	}
}

/**
 * 插入样板数据
 */
public void insertSampleData(String trackerId, String userId){
	RealTimeData data = new RealTimeData();
	data.setOrient("0");
	
	float lat1 = 4046.688400f;
	float lng1 = -74f - 1.878600f / 60f;
	data.setOrig_lat("4046.688400");
	data.setOrig_lng("0741.878600");
	data.setLng(lng1);
	data.setTrackerid(trackerId);
	data.setUserId(Integer.parseInt(userId));
	data.setState(AlarmType.GPS_Pos.toString());
	data.setSignal(AlarmType.GPS_Pos.toString());
	data.setLatOrient("N");
	data.setLngOrient("W");
	data.setDistance("-1000");
	Date d = new Date();
	data.setTraceDateTime(d);
	data.setOrig_lat(Float.toString(lat1));
	data.setLat(40.779648f);
	
	this.insertData(data);
	
}

/**判断该车是否连接上服务器
 * @param trackerid
 * @return 
 */
public boolean isConnected(String trackerid,List<RealTimeData> datas){
	SqlMapClient map = ClientFactory.getSqlMapper();
	RealTimeData data = new RealTimeData();
	data.setTrackerid(trackerid);
	data.setOrderCondition(" order by r.tracetime desc ");
	data.setTopCondition(" limit 0,1");
	try {
		log.error("datas is");
		RealTimeData result = (RealTimeData)map.queryForObject("selectRealTimeDataByExample", data);
		
		if(result == null){
			log.error("datas is null");
			return false;
		}
		if(datas != null){
			log.error("datas not null");
			datas.add(result);
			
			
			
		}
		return result.getSignal().equals(Signal.ok.name());
//		Date traceTime = result.getTraceDateTime();
//		Date now =new Date();
//		long passedSec = now.getTime() - traceTime.getTime();
//		if((int)(passedSec / 1000) < 6){  //  判断6秒内有实时数据，则为连接上
//			return true;
//		}
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}finally{
		try {
			map.endTransaction();
		} catch (SQLException e) {
			log.error("", e);
		}
	}
	return false;
}

/**获取所有车的连接状况
 * @param userid
 * @return
 */
public List<ConnectionTip> getAllConnectionTips(String userid){
	List<ConnectionTip> tips = new ArrayList();
	TrackerService ts = new TrackerService();
	List<Tracker> trackers = ts.findUserTracker(Integer.parseInt(userid));
	boolean isConnected = false;
	for(Tracker tracker: trackers){
		ConnectionTip tip = new ConnectionTip();
		isConnected = this.isConnected(tracker.getTrackerid(), null);
		tip.setTime(new Date());
		tip.setTrackerid(tracker.getTrackerid());
		tip.setConnected(isConnected);
		tips.add(tip);
	}
	return tips;
}

/**获取所有跟踪车的实时数据
 * @param trackers
 * @return
 */
public List<String> getTrackersRealTimeData(List<Tracker> trackers){
	List<String> r = new ArrayList();
	String gpsData = "";
	
	for(Tracker tracker: trackers){
		List<RealTimeData> rl = new ArrayList();
		this.isConnected(tracker.getTrackerid() , rl);
		if(rl.size() == 0){
			continue;
		}
		RealTimeData data = rl.get(0);
		gpsData = this.getDataString(data);
		
		r.add(gpsData);
	}
	return r;
}

public List<String> getTrackersAlarmData(List<Tracker> trackers){
	List<String> r = new ArrayList();
	String gpsData = "";
	
	for(Tracker tracker: trackers){
		RealTimeData search = new RealTimeData();
		search.setTrackerid(tracker.getTrackerid());
		search.setOrderCondition(" and length(r.state) > 0 order by traceTime desc ");
		List<RealTimeData> rl = findData("0", 20, search);
		if(rl.size() == 0){
			continue;
		}
		for(RealTimeData  data : rl){
			gpsData = this.getDataString(data);
			r.add(gpsData);
		}
	}
	return r;
}


public String getDataString(RealTimeData data){
	
	java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("ddMMyy");
	format.setTimeZone(TimeZone.getTimeZone("GMT"));
	java.text.SimpleDateFormat format2 = new java.text.SimpleDateFormat("HHmmss");
	format2.setTimeZone(TimeZone.getTimeZone("GMT"));
	String gpsData = data.getTracker().getUserTrackerId()+"="+
	data.getTrackerid()+"~"+this.getCommand(data)+"~";
	String[] info = new String[19];
	info[0] = "$GPRMC";
	info[1] = format2.format(data.getTraceDateTime());
	//info[2] = (isConnected?"A":"V"); // signal
	info[2] = (data.getSignal().equals(Signal.ok.name())?"A":"V"); // signal
	info[3] = data.getOrig_lat();
	info[4] = data.getLatOrient();
	info[5] = data.getOrig_lng();
	info[6] = data.getLngOrient();
	info[7] = Float.toString(data.getSpeed());
	info[8] = data.getOrient();
	info[9] = format.format(data.getTraceDateTime());
	info[10] = ""+data.getTracker().getMileage();
	info[11] = ""+data.getTracker().getAccStat();
	info[12] = ""+data.getTracker().getDoorStat();
	info[13] = "*14||24|";
	
	info[14] =data.getBattery();
	info[15] =data.getVoltage();
	info[16]=data.getFuel();
	info[17] =data.getAd2();
	info[18] =""+data.getTemp();
	
	for(int i = 0; i < info.length; i++){
		if(i == info.length - 1){
			gpsData = gpsData + info[i];
		}else{
			gpsData = gpsData + info[i]+",";
		}
	}
	gpsData = gpsData + "~"+data.getRealTimedataId()+"~";
	gpsData = gpsData+(data.getAddress()!=null? data.getAddress():"")+"~";
	return gpsData;
	
}
/**返回报警信息0表示位置GPS Pos，1表示SOS Alarm，0x10表示Low Power Alarm，0x11表示Over Speed Alarm
 * 12表示Position Move Alarm，0x13表示Geo Fence Alarm，0x14表示Reset Alarm，
 * 15表示No GPS Signal alarm，16表示Restore GPS Signal Alarm
 * @param data
 * @return
 */
private String getCommand(RealTimeData data) {
	String alarm = "0";
	if(data.getState().equals(AlarmType.GPS_Pos.toString())){
		alarm = DataFlag.POS;
	}else if(data.getState().equals(AlarmType.Over_Speed_Alarm.toString())){
		alarm = DataFlag.OVERSPEED;
	}else if(data.getState().equals(AlarmType.Geo_Fence_Alarm.toString())){
		alarm = DataFlag.GEO;
	}else if(data.getState().equals(AlarmType.Input_1.toString())){
		alarm = DataFlag.INPUT_1;
	}else if(data.getState().equals(AlarmType.Input_2.toString())){
		alarm = DataFlag.INPUT_2;
	}else if(data.getState().equals(AlarmType.Input_3.toString())){
		alarm = DataFlag.INPUT_3;
	}else if(data.getState().equals(AlarmType.Input_4.toString())){
		alarm = DataFlag.INPUT_4;
	}else if(data.getState().equals(AlarmType.Input_5.toString())){
		alarm = DataFlag.INPUT_5;
	}else if(data.getState().equals(AlarmType.External_Power_Lost.toString())){
		alarm = DataFlag.EXT_POWER;
	}else if(data.getState().equals(AlarmType.Low_Power_Alarm.toString())){
		alarm = DataFlag.LOW_POWER_ALARM;
	}else if(data.getState().equals(AlarmType.SOS_Alarm.toString())){
		alarm = DataFlag.SOS;
	}else if(data.getState().equals(AlarmType.ACC_ON.toString())){
//		alarm = DataFlag.ACC_ON;
	}else if(data.getState().equals(AlarmType.SIREN.toString())){
		alarm = DataFlag.SIREN_ALARM;
	}else if(data.getState().equals(AlarmType.DOOR_OPENED.toString())){
		//alarm = DataFlag.DOOR_OPENED;
	}
	
	return alarm;
}

public void updateAllLastDataSignal(Signal s){
	
	//String sql ="update RealTimeData set `signal`='"+s.toString()+"' ";
	String sql ="update RealTimeData set signal='"+s.toString()+"' ";//oracle sql
	if(Logger.getLogger(this.getClass()).isDebugEnabled())
		Logger.getLogger(this.getClass()).debug(sql);
	DataBase.update(sql);
	
	DataBase.closeConnection();	
}

public int updateLastDataSignal(Signal s, String trackerid){
	
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		
		RealTimeData data = new RealTimeData();
		data.setTrackerid(trackerid);
//		data.setOrderCondition("order by r.traceTime desc");
		data.setOrderCondition("order by r.traceTime desc ");
//		data.setOrderCondition(" and realtimedataid in (select max(rd.realtimedataid) from realtimedata rd where rd.trackerid='"+trackerid+"')");
		data.setTopCondition(" limit 0,1 ");
		RealTimeData result = (RealTimeData) map.queryForObject("selectRealTimeDataByExample", data);
		if(result == null){
			return 0;
		}
			
		log.debug("update signal for tracker "+trackerid+" `signal`="+s.name());
		if(s == Signal.ok){
			HashMap p = new HashMap();
			p.put("signal", s.name());
			p.put("realtimeid", result.getRealTimedataId());
			map.insert("updateLastDataSignal", p);
			result.setSignal(s.name());
		}else{
			String sql = "update realtimedata set `signal`='"+s.name()+"' where trackerid='"+trackerid+"'";
			DataBase.update(sql);
			DataBase.closeThreadConnection();
		}
		GeoDataOutput.getInstance().setRealtimeData(result.getTracker().getTrackerid(), this.getDataString(result));
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error("",e);
	}
	
	return 0;
}

public int TruncateRealtimeData(){
	Logger.getLogger(this.getClass()).debug("Truncating table realtimedata");
	String sql = "truncate table backup_RealTimeData";
	int r = DataBase.update(sql);
	sql = "select trackerid,trackername,usertrackerid from tracker";
	List<String> backupSql = new ArrayList<String>();
	try {
		ResultSet rs = DataBase.query(sql);
		while(rs.next()){
			log.debug("get realtime data, tracker id="+rs.getString(1));
			sql = "select * from realtimedata r where r.trackerid='"+rs.getString("trackerid")+"' and length(state)=0 order by tracetime desc limit 0,1";
			ResultSet rs2 = DataBase.query(sql);
			try{
				if(rs2.next()){
					backupSql.add(getBackupInsert(rs2));
				}
				rs2.close();
			}catch(Exception e){
				log.error("", e);
			}
		}
		
		log.debug("get realtime data alarm");
		sql = "select * from realtimedata where length(state)>0 order by tracetime desc";
		ResultSet rs2 = DataBase.query(sql);
		Map<String, Integer> alarmsCount = new HashMap<String, Integer>();
		try{
			while(rs2.next()){
				if(rs2.getString("state").equals(AlarmType.ACC_OFF.name())
						|| rs2.getString("state").equals(AlarmType.ACC_ON.name())
						|| rs2.getString("state").equals(AlarmType.DOOR_OPENED.name())
						|| rs2.getString("state").equals(AlarmType.DOOR_CLOSED.name())){
					continue;
				}
				String trackerId = rs2.getString("trackerid");
				String state = rs2.getString("state"); // 每个跟踪器每种报警保留5个
				Integer count = alarmsCount.get(trackerId+state);
				if(count == null || count < 5){ // 每个跟踪器每种报警保留5个
					alarmsCount.put(trackerId+state, count == null ? 1 : (count+1));
					backupSql.add(getBackupInsert(rs2));
				}
			}
			rs2.close();
		}catch(Exception e){
			log.error("", e);
		}
		sql = "truncate table RealTimeData";
		r = DataBase.update(sql);
		System.out.println("TruncateRealtimeData >> truncate result="+r);
		for(String s : backupSql){
			//System.out.println(s);
			r = DataBase.update(s);
		}
	} catch (SQLException e) {
		log.error("", e);
	}finally{
		DataBase.closeThreadConnection();
	}
	
	return r;
}

//private String getBackupInsert(ResultSet rs) throws SQLException {
//	final RealTimeData data = new RealTimeData();
//	data.setRealTimedataId(rs.getInt("realtimedataid"));
//	data.setOrient("0");
//	data.setOrig_lat(rs.getString("orig_lat"));
//	data.setLatOrient(rs.getString("latorient"));
//	data.setLat(rs.getFloat("lat"));
//	
//	data.setOrig_lng(rs.getString("orig_lng"));
//	data.setLng(rs.getFloat("lng"));
//	data.setLngOrient(rs.getString("lngorient"));
//	
//	data.setTrackerid(rs.getString("trackerid"));
//	data.setState(rs.getString("state"));
//	data.setSignal(rs.getString("signal"));
//	data.setSpeed(rs.getFloat("speed"));
//	
//	data.setDistance(rs.getString("distance"));
//	data.setTraceDateTime(rs.getTimestamp("tracetime"));
//	data.setLanguage(rs.getString("language"));
//	data.setAddress(rs.getString("address"));
//    
//	StringBuffer sb = new StringBuffer();
//	sb.append("insert into realtimedata(realtimedataid,trackerid,tracetime,address,orig_lat,latorient," +
//			"lngorient,orig_lng,lat,lng,speed,`signal`,orient,distance,state,language) values(");
//	sb.append(data.getRealTimedataId()+"");
//	sb.append(",'"+data.getTrackerid()+"'");
//	sb.append(",'"+data.getTraceDateTime()+"'");
//	sb.append(",'"+(data.getAddress() != null ? data.getAddress().replaceAll("'", "\\\\'"):"")+"'");
//	sb.append(",'"+data.getOrig_lat()+"'");
//	sb.append(",'"+data.getLatOrient()+"'");
//	sb.append(",'"+data.getLngOrient()+"'");
//	sb.append(",'"+data.getOrig_lng()+"'");
//	sb.append(","+data.getLat()+"");
//	sb.append(","+data.getLng()+"");
//	sb.append(","+data.getSpeed()+"");
//	sb.append(",'"+data.getSignal()+"'");
//	sb.append(",'0'");
//	sb.append(","+data.getDistance());
//	sb.append(",'"+data.getState()+"'");
//	sb.append(",'"+data.getLanguage()+"')");
//	return sb.toString();
//}


private String getBackupInsert(ResultSet rs) throws SQLException {
	final RealTimeData data = new RealTimeData();
	data.setRealTimedataId(rs.getInt("realtimedataid"));
	data.setOrient("0");
	data.setOrig_lat(rs.getString("orig_lat"));
	data.setLatOrient(rs.getString("latorient"));
	data.setLat(rs.getFloat("lat"));
	
	data.setOrig_lng(rs.getString("orig_lng"));
	data.setLng(rs.getFloat("lng"));
	data.setLngOrient(rs.getString("lngorient"));
	
	data.setTrackerid(rs.getString("trackerid"));
	data.setState(rs.getString("state"));
	data.setSignal(rs.getString("signal"));
	data.setSpeed(rs.getFloat("speed"));
	
	data.setDistance(rs.getString("distance"));
	data.setTraceDateTime(rs.getTimestamp("tracetime"));
	data.setLanguage(rs.getString("language"));
	data.setAddress(rs.getString("address"));
	
	data.setVoltage(rs.getString("voltage"));
	data.setBattery(rs.getString("battery"));
	data.setFuel(rs.getString("fuel"));
	data.setAd2(rs.getString("ad2"));
	data.setFlag(rs.getString("flag"));
	data.setTemp(rs.getString("temp"));
    
	StringBuffer sb = new StringBuffer();
	sb.append("insert into realtimedata(realtimedataid,trackerid,tracetime,address,orig_lat,latorient," +
			"lngorient,orig_lng,lat,lng,speed,`signal`,orient,distance,state,language,voltage,battery,fuel,ad2,flag,temp) values(");
	sb.append(data.getRealTimedataId()+"");
	sb.append(",'"+data.getTrackerid()+"'");
	sb.append(",'"+data.getTraceDateTime()+"'");
	sb.append(",'"+(data.getAddress() != null ? data.getAddress().replaceAll("'", "\\\\'"):"")+"'");
	sb.append(",'"+data.getOrig_lat()+"'");
	sb.append(",'"+data.getLatOrient()+"'");
	sb.append(",'"+data.getLngOrient()+"'");
	sb.append(",'"+data.getOrig_lng()+"'");
	sb.append(","+data.getLat()+"");
	sb.append(","+data.getLng()+"");
	sb.append(","+data.getSpeed()+"");
	sb.append(",'"+data.getSignal()+"'");
	sb.append(",'0'");
	sb.append(","+data.getDistance());
	sb.append(",'"+data.getState()+"'");
	sb.append(",'"+data.getLanguage()+"'");
	
	sb.append(",'"+data.getVoltage()+"'");
	sb.append(",'"+data.getBattery()+"'");
	sb.append(",'"+data.getFuel()+"'");
	sb.append(",'"+data.getAd2()+"'");
	sb.append(",'"+data.getFlag()+"'");
	sb.append(",'"+data.getTemp()+"')");
	
	return sb.toString();
}

public RealTimeData setSosAlarmFor3Party(Tracker tracker) throws SQLException {

		
		String sql = "select * from r_"+tracker.getUserTrackerId()+" order by createTime desc limit 0,1";
		ResultSet rs = DataBase.query(sql);
		if(rs.next()){
			RealTimeData data = new RealTimeData();
			data.setOrient(rs.getString("orient"));
			data.setOrig_lat(rs.getString("orig_lat"));
			data.setLatOrient(rs.getString("latorient"));
			data.setLat(rs.getFloat("lat"));
			
			data.setOrig_lng(rs.getString("orig_lng"));
			data.setLng(rs.getFloat("lng"));
			data.setLngOrient(rs.getString("lngorient"));
			
			data.setTrackerid(tracker.getTrackerid());
			data.setState(rs.getString("state"));
			data.setSignal(Signal.ok.name());
			data.setSpeed(rs.getFloat("speed"));
			
			data.setDistance("0");
			data.setTraceDateTime(new Date());
			data.setLanguage(rs.getString("language"));
			data.setAddress(rs.getString("address"));
			return data;
		}
		return null;
}


}
